package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.user.ReportDealerInfoRepository;
import com.tengju.data.domain.shared.Month;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.ReportDealerInfoMapper;
import com.tengju.user.domain.model.user.StarLevelEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class ReportDealerInfoRepositoryImpl implements ReportDealerInfoRepository {

    @Resource
    ReportDealerInfoMapper reportDealerInfoMapper;


    /**
     * 本月新增经销商数
     */
    @Override
    public List<Row> getCurrentMonthNewIncrease(Month curMonth, List<Long> secIdCodeList, Integer offset, Integer limit) {

        if (CollectionUtils.isEmpty(secIdCodeList)) {
            return Collections.emptyList();
        }

        List<Map<String,Object>> mapList =
                reportDealerInfoMapper.getMonthPullDealers(curMonth.getStartDate(),
                        curMonth.getEndDate().plusDays(1),
                        secIdCodeList,
                        offset,
                        limit);

        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }

        return mapList.stream().map(Row::new)
                .collect(Collectors.toList());
    }

    /***
     * 新增服务商数
     */
    @Override
    public List<Row> getStarSigningDealers(List<Long> secIdCodeList, List<Integer> signStepList, Integer offset, Integer limit) {

        if (CollectionUtils.isEmpty(secIdCodeList)) {
            return Collections.emptyList();
        }

        if (CollectionUtils.isEmpty(signStepList)) {
            return Collections.emptyList();
        }

        List<Map<String,Object>> mapList =
                reportDealerInfoMapper.getStarSigningDealers(secIdCodeList,signStepList,offset,limit);

        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream().map(Row::new)
                .collect(Collectors.toList());
    }

    /**
     * 即将晋升服务数
     */
    @Override
    public List<Row> getNotNoneStarDealers(List<Long> secIdCodeList,Integer offset, Integer limit) {

        if (CollectionUtils.isEmpty(secIdCodeList)) {
            return Collections.emptyList();
        }

        Integer starLevel = StarLevelEnum.NONE.getCode();

        List<Map<String,Object>> mapList =
                reportDealerInfoMapper.getTargetStarDealers(secIdCodeList,starLevel,offset,limit);

        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream().map(Row::new)
                .collect(Collectors.toList());
    }

    /**
     * 数据下载-服务商名单
     */
    @Override
    public List<Row> getDominationStarMember(Long idCode, Integer month, Integer offset, Integer limit) {
        if (idCode == null || month == null) {
            return Collections.emptyList();
        }

        List<Map<String,Object>> mapList =
                reportDealerInfoMapper.getDominationStarMember(idCode,month,offset,limit);

        if (CollectionUtils.isEmpty(mapList)) {
            return Collections.emptyList();
        }
        return mapList.stream().map(Row::new)
                .collect(Collectors.toList());
    }
}
